package idv.arthur.work;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QuerySysInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QuerySysInfo() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String errPars = "}";
		
		String strSQL = "";
		SysInfo si = new SysInfo();
		ArrayList<SystemList> sls = new ArrayList<SystemList>();
		ArrayList<DBConnection> dbcs = new ArrayList<DBConnection>();
		ArrayList<SVNConnection> svncs = new ArrayList<SVNConnection>();
		
		
		try {
			conn = wid.getConn(true);
			strSQL = "SELECT sysSN,sysname,isLocalSys FROM envsystemlist ORDER BY isLocalSys DESC,sysSN DESC";
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			
			while ( rs.next() ) {
				SystemList sl = new SystemList();
				sl.setSysNo( rs.getInt("sysSN") );
				sl.setSysName( wid.DB2UI( rs.getString("sysname"),conn.getMetaData().getDatabaseProductName() ) );
				sl.setLocalSys( "t".equalsIgnoreCase( rs.getString("isLocalSys") )?true:false);
				sls.add(sl);
			}
			si.setSles(sls);

			strSQL = "SELECT t1.dbLoginSN,t1.ip,t1.port,t1.id,t1.dbName,t2.DBTypeName FROM dblogin t1, dbtype t2 WHERE t1.DBTypeSN=t2.DBTypeSN";
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			while ( rs.next() ) {
				DBConnection dbc = new DBConnection();
				dbc.setDbConnSN(rs.getInt("dbLoginSN"));
				dbc.setDbTypeName(rs.getString("DBTypeName"));
				dbc.setDbName(rs.getString("dbName"));
				dbc.setId(rs.getString("id"));
				dbc.setIp(rs.getString("ip"));
				dbc.setPort(rs.getInt("port"));
				dbcs.add(dbc);
			}
			si.setDbcs(dbcs);
			
			strSQL = "SELECT t1.svnLoginSN,t1.loginURL,t1.id FROM svnlogin t1";
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			while ( rs.next() ) {
				SVNConnection svnc = new SVNConnection();
				svnc.setSvnConnSN(rs.getInt("svnLoginSN"));
				svnc.setId(rs.getString("id"));
				svnc.setUrl(rs.getString("loginURL"));
				svncs.add(svnc);
			}
			si.setSvncs(svncs);
			
		} catch ( SQLException e ) {
			wid.kem(conn, ps, e, wid.toEType("SQLException")+errPars, wid.isPrint2Console );
		} catch ( UnsupportedEncodingException e) {
			wid.kem(conn, ps, e, wid.toEType("UnsupportedEncodingException")+errPars, wid.isPrint2Console );
		} catch ( Exception e) {
			wid.kem(conn, ps, e, wid.toEType("Exception")+errPars, wid.isPrint2Console );
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				System.out.println(wid.errProcess(e));
			}
		}
		
		response.getWriter().write(new Gson().toJson(si));	
	}
}